package cn.iocoder.yudao.module.polling.controller.admin.pollingpath;

import cn.iocoder.yudao.module.polling.controller.admin.pollingpath.vo.*;
import cn.iocoder.yudao.module.polling.convert.pollingpath.PollingPathConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingpath.PollingPathDO;
import cn.iocoder.yudao.module.polling.service.pollingpath.PollingPathService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
@Api(tags = "管理后台 - 巡检路线")
@RestController
@RequestMapping("/abu/polling-path")
@Validated
public class PollingPathController {

    @Resource
    private PollingPathService pollingPathService;

    @PostMapping("/create")
    @ApiOperation("创建巡检路线")
    @PreAuthorize("@ss.hasPermission('abu:polling-path:create')")
    public CommonResult<Integer> createPollingPath(@Valid @RequestBody PollingPathCreateReqVO createReqVO) {
        return success(pollingPathService.createPollingPath(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新巡检路线")
    @PreAuthorize("@ss.hasPermission('abu:polling-path:update')")
    public CommonResult<Boolean> updatePollingPath(@Valid @RequestBody PollingPathUpdateReqVO updateReqVO) {
        pollingPathService.updatePollingPath(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除巡检路线")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-path:delete')")
    public CommonResult<Boolean> deletePollingPath(@RequestParam("id") Integer id) {
        pollingPathService.deletePollingPath(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得巡检路线")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-path:query')")
    public CommonResult<PollingPathRespVO> getPollingPath(@RequestParam("id") Integer id) {
        PollingPathDO pollingPath = pollingPathService.getPollingPath(id);
        return success(PollingPathConvert.INSTANCE.convert(pollingPath));
    }

    @GetMapping("/list")
    @ApiOperation("获得巡检路线列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-path:query')")
    public CommonResult<List<PollingPathRespVO>> getPollingPathList(@RequestParam("ids") Collection<Integer> ids) {
        List<PollingPathDO> list = pollingPathService.getPollingPathList(ids);
        return success(PollingPathConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得巡检路线分页")
    @PreAuthorize("@ss.hasPermission('abu:polling-path:query')")
    public CommonResult<PageResult<PollingPathRespVO>> getPollingPathPage(@Valid PollingPathPageReqVO pageVO) {
        PageResult<PollingPathDO> pageResult = pollingPathService.getPollingPathPage(pageVO);
        return success(PollingPathConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出巡检路线 Excel")
    @PreAuthorize("@ss.hasPermission('abu:polling-path:export')")
    @OperateLog(type = EXPORT)
    public void exportPollingPathExcel(@Valid PollingPathExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PollingPathDO> list = pollingPathService.getPollingPathList(exportReqVO);
        // 导出 Excel
        List<PollingPathExcelVO> datas = PollingPathConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检路线.xls", "数据", PollingPathExcelVO.class, datas);
    }

}
